package com.ellricky.teacher.ui.mycourse.course

import android.widget.Toast
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.andr.common.ui.activity.BaseToolBarActivity
import com.andr.common.ui.mvp.BasePresenter
import com.ellricky.baselib.cache.LocalCacheUtil
import com.ellricky.baselib.entity.new.School
import com.ellricky.baselib.entity.new.StaffRank
import com.ellricky.baselib.http.HttpUtils
import com.ellricky.baselib.ui.TYPE_MONTH
import com.ellricky.baselib.util.EAppUtil
import com.ellricky.baselib.util.NewEListCallback
import com.ellricky.teacher.MyCourseAdapter
import com.ellricky.teacher.R
import kotlinx.android.synthetic.main.tc_activity_change_school.*

class ChangeSchoolActivity : BaseToolBarActivity<Void, BasePresenter<Void>>() {

    private val schoolAdapter by lazy {
        MyCourseAdapter.ChangeSchoolAdapter(this@ChangeSchoolActivity)
    }

    override fun getContentLayout(): Int {
        return R.layout.tc_activity_change_school
    }

    override fun initUIView() {
        super.initUIView()

        mTvTitle.text = "切换校区"

        with(rvSchool) {
            layoutManager = GridLayoutManager(this@ChangeSchoolActivity, 3)
            adapter = schoolAdapter
        }

        schoolAdapter.setOnItemClickListener { _,
                                               _,
                                               position,
                                               _ ->
            val school = schoolAdapter.get(position)
            LocalCacheUtil.setSchoolId(school.school_id)
            Toast.makeText(this, "切换校区成功", Toast.LENGTH_LONG).show()
            finish()
        }

        loadData()
    }

    private fun loadData() {
        val url = EAppUtil.getApiHost()
        val paramMap = mutableMapOf(
            "action" to "teacherSchoolList",
            "eid" to LocalCacheUtil.getUid()
        )

        HttpUtils.getInstance()
            .postFrom(url, paramMap, object : NewEListCallback<School>(this) {
                override fun onSuccess(data: MutableList<School>?, msg: String?) {
                    schoolAdapter.refresh(data)
                }
            })
    }
}